package com.hubei.listener; /**
 * @author PJM
 * @date 2025/6/25-9:25
 */

import com.hubei.dao.DepartmentDao;
import com.hubei.dao.DictDao;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.Map;

@WebListener()
public class ApplicationListener implements ServletContextListener{

    // Public constructor is required by servlet spec
    public ApplicationListener() {
    }

    // -------------------------------------------------------
    // ServletContextListener implementation
    // -------------------------------------------------------
    public void contextInitialized(ServletContextEvent sce) {
      /*
        读取字典表、部门表中的两个数据，存储于全局对象application中
      */
        ServletContext application = sce.getServletContext();
        DictDao dictDao = DictDao.getInstance();
        Map<String, String> jobMap = dictDao.getJobType();
        application.setAttribute("jobMap", jobMap);

        DepartmentDao departDao = DepartmentDao.getInstance();
        Map<String, String> departMap = departDao.getDepartment();
        application.setAttribute("departMap", departMap);
    }

    public void contextDestroyed(ServletContextEvent sce) {
      /* This method is invoked when the Servlet Context 
         (the Web application) is undeployed or 
         Application Server shuts down.
      */
    }

}
